/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.web.merchant;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;
import com.neusoft.lohas.modules.ivm.entity.merchant.DeliveryFee;
import com.neusoft.lohas.modules.ivm.service.merchant.DeliveryFeeService;

/**
 * 配送费Controller
 * @author Jackie.Liu
 * @version 2015-04-16
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/delivery/fee")
public class DeliveryFeeController extends BaseController {

	@Autowired
	private DeliveryFeeService deliveryFeeService;
	
	@ModelAttribute
	public DeliveryFee get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return deliveryFeeService.get(id);
		}else{
			return new DeliveryFee();
		}
	}
	
	@RequiresPermissions("ivm:delivery:fee:view")
	@RequestMapping(value = {"list", ""})
	public String list(DeliveryFee deliveryFee, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			deliveryFee.setCreateBy(user);
		}
        Page<DeliveryFee> page = deliveryFeeService.find(new Page<DeliveryFee>(request, response), deliveryFee); 
        model.addAttribute("page", page);
		return "modules/ivm/deliveryFeeList";
	}

	@RequiresPermissions("ivm:delivery:fee:view")
	@RequestMapping(value = "form")
	public String form(DeliveryFee deliveryFee, Model model) {
		if (StringUtils.isBlank(deliveryFee.getId())){
			deliveryFee.setMerchant(super.merchant);
			deliveryFee = deliveryFeeService.findByMerchantId(deliveryFee.getMerchant().getId());
		}
		model.addAttribute("deliveryFee", deliveryFee);
		return "modules/ivm/set/deliveryFeeForm";
	}

	@RequiresPermissions("ivm:delivery:fee:edit")
	@RequestMapping(value = "save")
	public String save(DeliveryFee deliveryFee, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, deliveryFee)){
			return form(deliveryFee, model);
		}
		deliveryFeeService.save(deliveryFee);
		addMessage(redirectAttributes, "保存配送费成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/delivery/fee/form";
	}
	
	@RequiresPermissions("ivm:delivery:fee:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		deliveryFeeService.delete(id);
		addMessage(redirectAttributes, "删除配送费成功");
		return "redirect:"+Global.getAdminPath()+"/modules/ivm/deliveryFee/?repage";
	}

}
